package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.pojo.Tuihuo;
import com.hqyj.dao.TuihuoMapper;
import com.hqyj.service.ITuihuoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * <p>
 * 退货记录 服务实现类
 * </p>
 *
 * @author rock
 * @since 2021-10-21
 */
@Service
public class TuihuoServiceImpl implements ITuihuoService {

    //注入TuihuoMapper
    @Autowired(required = false)
    TuihuoMapper tuihuoMapper;

    @Override
    public HashMap<String, Object> select(Tuihuo tuihuo) {
        //创建HashMap对象
        HashMap<String,Object> map = new HashMap<>();
        //创建分页对象
        Page<Tuihuo> page = new Page<>(tuihuo.getPage(),tuihuo.getRow());

        //定义条件构造器
        QueryWrapper<Tuihuo> queryWrapper = new QueryWrapper<>();
        //查询数据库
        Page<Tuihuo> rowsPageList = tuihuoMapper.selectPage(page, queryWrapper);
        //定义当前页集合
        map.put("list",rowsPageList.getRecords());
        //总页数
        map.put("totalPage",rowsPageList.getPages());
        //总条数
        map.put("total",rowsPageList.getTotal());
        //当前页
        map.put("curPage",tuihuo.getPage());
        //如果当前页为1，则上一页为也为1
        if (rowsPageList.getCurrent() == 1){
            //上一页
            map.put("prePage",1);
        }else {
            map.put("prePage",tuihuo.getPage()-1);
        }
        //如果当前页为最后一页，则下一页为也为最大页
        if (tuihuo.getPage() == rowsPageList.getPages()){
            //下一页
            map.put("nextPage",rowsPageList.getPages());
        }else {
            map.put("nextPage",tuihuo.getPage()+1);
        }
        //显示每页条数
        map.put("row",tuihuo.getRow());
        return map;
    }

    @Override
    public HashMap<String, Object> del(int tuihuoId) {
        //创建HashMap对象
        HashMap<String,Object> map = new HashMap<>();
        int num = tuihuoMapper.deleteById(tuihuoId);
        if (num > 0){
            map.put("info","保存成功");
        }else {
            map.put("info","保存失败");
        }
        return map;
    }
}
